package web;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.StudentServices;
import dto.Student;

/**
 * Servlet implementation class SearchStudentController
 */
@WebServlet(urlPatterns="/DEO/searchStudent")
public class SearchStudentController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private StudentServices studentServices;
	
	public void init()
	{
		studentServices = new StudentServices();
	}
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchStudentController() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    @Override
	public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
	{
		String criteria = req.getParameter("criteria");
		String path="";
		
		if(criteria.equals("SID"))
		{
			int sid = Integer.parseInt(req.getParameter("key"));
			Student s = studentServices.findStudent(sid);
			req.setAttribute("student", s);
			path="StudentDetail.jsp";
		}
		else {
			String namePattern = req.getParameter("key");
			Collection <Student> students = studentServices.findAllStudentByName(namePattern);
			req.getSession().setAttribute("students", students);
			path="MultiStudentDetail.jsp";
		}
		RequestDispatcher rd = req.getRequestDispatcher(path);
		rd.forward(req, res);
	} // end services

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
